package com.note.chh.notebook.UI.enjoy;

import android.os.Bundle;
import android.os.Environment;
import android.view.KeyEvent;
import android.widget.TextView;

import com.blankj.utilcode.util.NetworkUtils;
import com.bumptech.glide.Glide;
import com.luck.picture.lib.photoview.PhotoView;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.FileCallback;
import com.lzy.okgo.model.Response;
import com.note.chh.notebook.Api.Constant;
import com.note.chh.notebook.R;
import com.note.chh.notebook.UI.base.BaseActivity;
import com.note.chh.notebook.Uitls.FileUtils;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

//查看音乐专辑图片 - 保存图片
public class CheckMusicPictureActivity extends BaseActivity {

    @BindView(R.id.mPhoto_pic)
    PhotoView mPhotoPic;
    @BindView(R.id.mText_savePic)
    TextView mTextSavePic;

    private String picUrl = "";//图片地址
    private String savePicName = "";//保存的图片的名字
    private String saveFolder = "";//保存图片的文件夹

    @Override
    protected int bindLayout() {
        return R.layout.activity_check_music_picture;
    }

    @Override
    public void initView() {
        statusBarVisible(false);
        Bundle bundle = getIntent().getExtras();
        picUrl = bundle.getString("picUrl");
        savePicName = bundle.getString("picName");
        saveFolder = bundle.getString("picFolder");
        Glide.with(this).load(picUrl).into(mPhotoPic);

    }

    @Override
    public void initData() {

    }


    @OnClick(R.id.mText_savePic)
    public void onViewClicked() {
        File file = new File(FileUtils.getSavePath(saveFolder)+savePicName);
        if (file.exists()) {
            showToast(CheckMusicPictureActivity.this,"图片已存在");
        }else{
            saveImage();
        }
    }
    //保存图片
    private void saveImage(){
        OkGo.<File>get(picUrl)
                .tag(this)
                .execute(new FileCallback(FileUtils.getSavePath(saveFolder),savePicName){
                    @Override
                    public void onSuccess(Response<File> response) {

                        showToast(CheckMusicPictureActivity.this,"图片保存在:"+ Environment.getExternalStorageDirectory().getAbsolutePath()+saveFolder);
                    }

                    @Override
                    public void onError(Response<File> response) {
                        super.onError(response);
                        if (!NetworkUtils.isConnected()) {
                            showToast(CheckMusicPictureActivity.this,Constant.NOTNETWORKCONNECT);
                        } else if (!NetworkUtils.isAvailableByPing()) {
                            showToast(CheckMusicPictureActivity.this,Constant.NETWORKNOUSER);
                        }else{
                            showToast(CheckMusicPictureActivity.this,Constant.saveError);
                        }
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        statusBarVisible(true);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (saveFolder.equals(Constant.imageSaveFolder)) {
            startActivity(PlayMusicActivity.class);
        }
        overridePendingTransition(R.anim.alpha_in, R.anim.alpha_out);
    }


}
